import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'

// 导入视图组件
import HomeView from '@/views/HomeView.vue' 
import LoginView from '@/views/User/LoginView.vue'
import RegisterView from '@/views/User/RegisterView.vue'
import ProfileView from '../views/User/ProfileView.vue'
import SleepView from '@/views/Features/SleepView.vue'
import ExerciseView from '@/views/Features/ExerciseView.vue'
import DietView from '@/views/Features/DietView.vue'
import FitnessView from '@/views/Features/FitnessView.vue'
import ForumPostCreate from '@/views/ForumPostCreate.vue'
import ForumPostDetail from '@/views/ForumPostDetail.vue'
import ChronicDiseaseCreate from '@/views/ChronicDiseaseCreate.vue'

Vue.use(VueRouter)

const routes = [
    {
    path: '/',
    name: 'Login',
    component: LoginView
    },
    {
      path: '/register',
      name: 'Register',
      component: RegisterView
  },
  {
    path: '/home', // 根路径指向 HomeView
    name: 'Home',
    component: HomeView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/profile',
    name: 'profile',
    component: ProfileView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/profile-form',
    name: 'ProfileForm',
    component: () => import('@/views/User/UserProfileForm.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/sleep',
    name: 'Sleep',
    component: SleepView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/exercise',
    name: 'Exercise',
    component: ExerciseView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/diet',
    name: 'Diet',
    component: DietView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/fitness',
    name: 'Fitness',
    component: FitnessView,
    meta: { requiresAuth: true, requiresProfile: true }
  },
  {
    path: '/forum/create',
    name: 'ForumPostCreate',
    component: ForumPostCreate,
    meta: { requiresAuth: true }
  },
  {
    path: '/forum/post/:id',
    name: 'ForumPostDetail',
    component: ForumPostDetail,
    meta: { requiresAuth: true }
  },
  {
    path: '/chronic-disease/create',
    name: 'ChronicDiseaseCreate',
    component: ChronicDiseaseCreate,
    meta: { requiresAuth: true }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 公开路由列表
  const publicPages = ['/', '/register'];
  // 检查目标路由是否需要认证
  const authRequired = !publicPages.includes(to.path);
  
  // 从sessionStorage获取用户信息并解析
  const userStr = sessionStorage.getItem('userInfo');
  let user = null;
  
  try {
    // 尝试解析用户信息
    if (userStr) {
      user = JSON.parse(userStr);
    }
  } catch (e) {
    console.error('解析用户信息失败:', e);
  }
  
  // 检查是否有有效的用户ID
  const isLoggedIn = user && user.id;
  
  // 如果需要认证但未登录
  if (authRequired && !isLoggedIn) {
    console.log('需要登录，但用户未登录，重定向到登录页');
    // 显示提示
    Vue.prototype.$message({
      message: '请先登录',
      type: 'warning'
    });
    // 重定向到登录页
    return next('/');
  }
  
  // 不需要认证或已经登录，直接放行
  return next();
});

export default router
